// pages/search/index.js
import {
  list
} from '../../../api/product'
let app = getApp();
Page({

  /**
   * 页面的初始数据
   */
  data: {
    brandList:[],
    isProduct:false,
    maxPrice:'',
    minPrice:'',
    isScreen:false,
    brandId:'',
    goods:'',
    sClick:false,
    pClick:false,
    searchList:[],
    historyList:[],
    focus:false,
    pageNum:1,
    status:false,
    showType:'transverse',
    nClick:true,
    isNullShow:false,
    isTitleShow:'',
    isShow:false
  },
  empty(){
      this.data.goods=''
      this.data.isShow=false
      this.data.searchList=[]
      this.setData({
        searchList:this.data.searchList,
        isShow:this.data.isShow,
        goods:this.data.goods,
      })
  },
  searchGoos(e){
    this.data.brandList=e.detail.brandList,
    this.data.isNullShow=e.detail.isNull,
    this.data.minPrice=e.detail.minPrice,
    this.data.brandId=e.detail.brandId,
    this.data.maxPrice=e.detail.maxPrice,
    this.data.sClick=e.detail.isShow
    this.data.searchList=e.detail.products
    this.data.isScreen=e.detail.isScreen
    this.setData({
      brandList:this.data.brandList,
      isNullShow:this.data.isNullShow,
      minPrice:this.data.minPrice,
      maxPrice:this.data.maxPrice,
      isScreen:this.data.isScreen,
      sClick:this.data.sClick,
      searchList:this.data.searchList
    })
  },
  getProduct(e){
    console.log(this.data.isProduct)
    this.data.isProduct=e.detail.isProduct,
    this.data.brandList=e.detail.brandList
    this.data.isTitleShow=e.detail.showSearch
    this.data.goods=e.detail.productName
    this.data.searchList=e.detail.products
    if(this.data.searchList.length==0){
      this.data.isNullShow=true
    }else{
      this.data.isNullShow=false
    }
    this.setData({
      brandList:this.data.brandList,
      isProduct:this.data.isProduct,
      isTitleShow:this.data.isTitleShow,
      goods:this.data.goods,
      isNullShow:this.data.isNullShow,
      searchList:this.data.searchList
    })
  },
  searchGoods(data){
    var hash = {};
    this.data.goods=data.detail.value.replace(/\s+/g, '')
    this.data.searchList=[]
    this.data.brandList=[]
    if(data.detail.value.length!=0&&this.data.goods!=""){
      list({
        name:data.detail.value,
        mchId: app.globalData.mchId
      }).then(res=>{
        console.info(res)
        this.data.searchList=res.data.data
        this.data.searchList.forEach(item=>{
          let brand={
            brandId:item.brandId,
            brandName:item.brandName,
            isActive:false
          }
          this.data.brandList.push(brand)
        })
      
      	this.data.brandList = this.data.brandList.reduce((item, next) => {
					hash[next.brandName] ? '' : hash[next.brandName] = true && item.push(next);
					return item
        }, [])
    
        if(this.data.searchList.length>0){
          this.data.isTitleShow=false
        }
        
        this.setData({ 
          brandList:this.data.brandList,
          isTitleShow: this.data.isTitleShow,
          goods:this.data.goods,
          searchList:this.data.searchList
        })
      })
    }else if(data.detail.value.length==0){
      this.data.searchList=[]
      this.setData({
        searchList:this.data.searchList,
        goods:this.data.goods
      })
    }
    if(this.data.goods.length!=0){
      this.data.isShow=true
    }else{
      this.data.isShow=false
    }
    this.setData({
      isShow: this.data.isShow
    })
  },
  cancelClick(){
    wx.navigateBack({
      delta: 1,
    })
  },
  setNavSize: function () {
    var that = this,
      sysinfo = wx.getSystemInfoSync(),
      statusHeight = sysinfo.statusBarHeight,
      isiOS = sysinfo.system.indexOf('iOS') > -1,
      navHeight;
      console.log(sysinfo)
    if (!isiOS) {
      navHeight = 46;
    } else {
      navHeight = 42;
    }
    that.setData({
      statuss: statusHeight,
      navHeight: navHeight
    })
  },

  toItem(e){
     var hash = {};
    this.data.isScreen=false
    this.data.historyList.unshift(e.target.dataset.name)
      this.data.historyList= this.data.historyList.reduce((item, next) => {
                hash[next] ? '' : hash[next] = true && item.push(next);
                return item
              }, [])
    wx.setStorage({
      key:"history",
      data:this.data.historyList
    })
      list({
        name:e.target.dataset.name,
        mchId: app.globalData.mchId
      }).then(res=>{
        this.data.searchList=res.data.data
        this.data.searchList.forEach(item=>{
          let brand={
            brandId:item.brandId,
            brandName:item.brandName,
            isActive:false
          }
          this.data.brandList.push(brand)
        })
      	this.data.brandList = this.data.brandList.reduce((item, next) => {
					hash[next.brandName] ? '' : hash[next.brandName] = true && item.push(next);
					return item
        }, [])
        if(this.data.searchList.length>0){
          this.data.isProduct=true
        }else{
          this.data.isProduct=false
        }
        this.data.isTitleShow=true
        this.setData({
          brandList:this.data.brandList,
          historyList: this.data.historyList,
          isProduct:this.data.isProduct,
          isScreen:this.data.isScreen,
          searchList:this.data.searchList,
          isTitleShow:this.data.isTitleShow
        })

      })
  },
  onPullDownRefresh() {
    wx.showNavigationBarLoading()
    setTimeout(() => {
      wx.hideNavigationBarLoading()
      this.data.pageNum=1
      list({
        pageNum:this.data.pageNum,
        name:this.data.goods,
        mchId: app.globalData.mchId,
      }).then(res=>{  
        this.data.searchList=res.data.data
        this.setData({
          searchList:this.data.searchList
        })
      })
      wx.stopPullDownRefresh()
    }, 1000);
  },

  onReachBottom() {
    this.data.pageNum+=1
    if(this.data.pageNum==1){
      this.data.searchList=[]
    }
    list({
      maxPrice:this.data.maxPrice,
      minPrice:this.data.minPrice,
      pageNum:this.data.pageNum,
      brandId:this.data.brandId,
      name:this.data.goods,
      mchId: app.globalData.mchId,
    }).then(res=>{  
      res.data.data.forEach(item=>{
        this.data.searchList.push(item)
      })
      this.setData({
        searchList:this.data.searchList
      })
    })
  },
  delHistory(){ 
    var _this=this
    wx.removeStorage({
      key: 'history',
      success (res) {
        _this.data.historyList=[]
        _this.data.status=false
        _this.setData({
            status: _this.data.status,
            historyList:_this.data.historyList
        })
      }
    })
  },
  bindconfirm(e){
     var hash = {};
    this.data.isScreen=false
    var that=this
    that.data.historyList.unshift(e.detail.value)
    that.data.status=true
      this.data.historyList= this.data.historyList.reduce((item, next) => {
          hash[next] ? '' : hash[next] = true && item.push(next);
          return item
        }, [])
    that.setData({
      status:that.data.status,
      historyList: that.data.historyList
    })
    wx.setStorage({
      key:"history",
      data:that.data.historyList
    })
    if(this.data.searchList.length>0){
      this.data.isProduct=true
    }else{
      this.data.isProduct=false
    }
    if(this.data.goods.length!=0 && this.data.searchList.length==0){
        this.data.isNullShow=true
        this.data.isTitleShow=false
    }else if(this.data.goods.length!=0 && this.data.searchList.length!=0){
      this.data.isTitleShow=true
      this.data.isNullShow=false
    }
    this.setData({
      isProduct:this.data.isProduct,
      isScreen:this.data.isScreen,
      isNullShow:this.data.isNullShow,
      isTitleShow:this.data.isTitleShow
    })
  },
  showType(){
    if(this.data.showType=='transverse'){
      this.data.showType='portrait'
    }else if(this.data.showType=='portrait'){
      this.data.showType='transverse'
    }
    this.setData({
      showType:this.data.showType
    })
  },
  nClick(){
    this.data.nClick=true
    this.data.pClick=false
    list({
      maxPrice:this.data.maxPrice,
      minPrice:this.data.minPrice,
      brandId:this.data.brandId,
      name:this.data.goods,
      mchId: app.globalData.mchId,
    }).then(res=>{  
      this.data.searchList=res.data.data
      this.setData({
        searchList:this.data.searchList
      })
    })
    this.setData({
      pClick:this.data.pClick,
      nClick:this.data.nClick
    })
  },
  isFocus(){
    if(this.data.searchList.length>0&&this.data.isTitleShow==true){
        this.data.isTitleShow=false
    }else if(this.data.isNullShow ==true){
      this.data.isNullShow = false
    }
    this.data.focus=true
    this.setData({
      isNullShow:this.data.isNullShow,
      isTitleShow:this.data.isTitleShow,
      focus:this.data.focus
    })
  },

  pClick(){
    this.data.pClick=true
    this.data.nClick=false
    if( this.data.pClick==true){
      list({
        maxPrice:this.data.maxPrice,
        minPrice:this.data.minPrice,
        brandId:this.data.brandId,
        name:this.data.goods,
        mchId: app.globalData.mchId,
        totalSale:'totalSale'
      }).then(res=>{  
        this.data.searchList=res.data.data
        this.setData({
          searchList:this.data.searchList
        })
      })
    }
    this.setData({
      pClick:this.data.pClick,
      nClick:this.data.nClick
    })
  },
  sClick(){
    this.data.sClick=true
    this.setData({
      sClick:this.data.sClick
    })
  },
  
  mask (e){
	  this.setData({ sClick: e.detail.sClick })
  },
  /**
   * 生命周期函数--监听页面加载
   */
  onLoad: function (options) {
    this.setNavSize()
    var _this=this
    wx.getStorage({
      key: 'history',
      success:function(res){
        console.log(res.data)
        _this.data.historyList=res.data
        if(_this.data.historyList.length>0){
          _this.data.status=true
        }else{
          _this.data.status=false
        }
        _this.setData({
          historyList:_this.data.historyList,
          status:_this.data.status
        })
      }
    })
    this.setData({
      statusBarHeight: app.globalData.statusBarHeight,
    });
  },

  
})